import router from "../router/index"
// 获取url参数
export const getUrlParams = function () {
  let url = location.href //获取url中"?"符后的字串
  let theRequest = new Object()
  if (url.indexOf('?') != -1) {
    let str = url.split('?')[1]
    let strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
    }
  }
  return theRequest
}

// export const backApp = function () {
//   router.back()
// }
export const appRequest = function (data, types) {
  try {
    var u = navigator.userAgent;
    var todata = { types: types, data: data };

    if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) {
      JsBridge.appRequest(JSON.stringify(todata));
    }
    if (u.indexOf("iPhone") > -1 || u.indexOf("iOS") > -1) {
      window.webkit.messageHandlers.appRequest.postMessage(
        JSON.stringify(todata)
      );
    }
  } catch (error) {
    console.log("appRequest===", error);
    if (types == 0 || types == 1) {
      router.go(-1);
    }
  }
}

export const isPhoneType = function (typeValue) {
  let result = false
  let type = false
  let rate = window.screen.height / window.screen.width
  let limit = window.screen.height == window.screen.availHeight ? 1.8 : 1.65 // 临界判断值

  // window.screen.height为屏幕高度
  //  window.screen.availHeight 为浏览器 可用高度
  if (rate > limit) {
    // 全面屏
    result = true
  }

  let u = navigator.userAgent

  if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
    type = 'Android'
  }
  if (u.indexOf('iPhone') > -1 || u.indexOf('iOS') > -1) {
    type = 'ios'
  }

  if (typeValue === 'isFullScreen') {
    return result
  }

  if (typeValue === 'ios' || typeValue === 'Android') {
    return typeValue === type
  }

  return {
    isFullScreen: result,
    type: type,
  }
}